package com.toaa.crazymath.controller;

import java.util.Random;

import com.toaa.crazymath.Constant.GameMode;

public abstract class BaseOperator {
	
	protected Random mRandom;
	protected int mFactor1;
	protected int mFactor2;
	protected int mResult;
	
	public BaseOperator() {
		mRandom = new Random();
	}
	
	protected abstract int randomNumber();
	protected void getActualResult(final int correctResult) {
		int wrongResult;
		mResult = correctResult;
		if (mRandom != null) {
			if (mRandom.nextBoolean()) {
				mResult = correctResult;
			} else {
				int min = correctResult - 5;
				while (min < 0) {
					min += 1;
				}
				final int max = correctResult + 5;
				wrongResult = randomNumberInRange(min, max);
				mResult = wrongResult;
			}
		}
	}
	public abstract OperatorEntity createExpression();
	public int randomNumberInRange(int min, int max) {
	    int randomNum = mRandom.nextInt((max - min) + 1) + min;
	    return randomNum;
	}
	
	public boolean checkExpression(int modeId) {
		boolean isCorrect = false;
		if (modeId == GameMode.ADDITION.ordinal()) {
			isCorrect = mResult == (mFactor1 + mFactor2);
		} else if (modeId == GameMode.SUBTRACTION.ordinal()) {
			isCorrect = mResult == (mFactor1 - mFactor2);
		} else if (modeId == GameMode.MULTIPLICATION.ordinal()) {
			isCorrect = mResult == (mFactor1 * mFactor2);
		} else {
			isCorrect = mResult == (mFactor1 / mFactor2);
		}
		return isCorrect;
	}
}
